﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

[TestClass()]
public class SplitMergeBinaryFilesTest
{
    [TestMethod()]
    public void SplitHTMLFile()
    {
        FileInfo originalFile = new FileInfo(@"E:\DOWNLOADS\SplitTest\Diablo II Install Log.html");
        SplitMergeBinaryFiles.SplitFile(originalFile.FullName);
        Assert.IsTrue(File.Exists(originalFile.FullName+".part1"));
        FileInfo part1 = new FileInfo(originalFile.FullName + ".part1");
        FileInfo part2 = new FileInfo(originalFile.FullName + ".part2");
        Assert.AreEqual(originalFile.Length / 2, part1.Length);
        Assert.AreEqual(part1.Length + part2.Length, originalFile.Length);
        Assert.IsTrue(Math.Abs(part1.Length - part2.Length) <= 1);
    }

    [TestMethod()]
    public void SplitMp3File()
    {
        FileInfo originalFile = new FileInfo(@"E:\DOWNLOADS\SplitTest\Spiderbait - Black Betty.mp3");
        SplitMergeBinaryFiles.SplitFile(originalFile.FullName);
        Assert.IsTrue(File.Exists(originalFile.FullName + ".part1"));
        FileInfo part1 = new FileInfo(originalFile.FullName + ".part1");
        FileInfo part2 = new FileInfo(originalFile.FullName + ".part2");
        Assert.AreEqual(originalFile.Length / 2, part1.Length);
        Assert.AreEqual(part1.Length + part2.Length, originalFile.Length);
        Assert.IsTrue(Math.Abs(part1.Length - part2.Length) <= 1);
    }

    [TestMethod()]
    [DeploymentItem("SplitMergeBinaryFiles.dll")]
    public void MergeFilesTest()
    {
        string part1Path = @"E:\DOWNLOADS\SplitTest\Spiderbait - Black Betty.mp3.part1"; 
        string part2Path = @"E:\DOWNLOADS\SplitTest\Spiderbait - Black Betty.mp3.part2";
        FileInfo part1 = new FileInfo(part1Path);
        FileInfo part2 = new FileInfo(part2Path);
        bool mergeResult = SplitMergeBinaryFiles.MergeFiles(part1Path, part2Path);
        Assert.IsTrue(mergeResult);
        Assert.IsTrue(File.Exists(part1Path+".restored"));
        FileInfo result = new FileInfo(part1Path + ".restored");
        Assert.AreEqual(result.Length, part1.Length + part2.Length);
    }

    [DeploymentItem("SplitMergeBinaryFiles.dll")]
    public void MergeNonExistingFiles()
    {
        string part1Path = @"E:\DOWNLOADS\SplitTest\Spid2erbait - Black Betty.mp3.part1";
        string part2Path = @"E:\DOWNLOADS\SplitTest\Spiderbait - Black Betty.mp3.part2";        
        bool mergeResult = SplitMergeBinaryFiles.MergeFiles(part1Path, part2Path);
        Assert.IsFalse(mergeResult);        
    }
}